diff --git a/example/lib/main.dart b/example/lib/main.dart index ca592d6cbf7efba496983a30c4f300553e40701b..69ff2379a003f8177fb50159e796e058e315af07 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -1,9 +1,24 @@ +// +// Copyright (c) 2024 Hunan OpenValley Digital Industry Development Co., Ltd. +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + import 'dart:math'; import 'package:audio_session/audio_session.dart'; import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; -import 'package:just_audio/just_audio.dart' as ja; +import 'package:audioplayers/audioplayers.dart'; void main() { runApp(MyApp()); @@ -15,12 +30,10 @@ class MyApp extends StatefulWidget { } class _MyAppState extends State { - final _player = ja.AudioPlayer( - // Handle audio_session events ourselves for the purpose of this demo. - handleInterruptions: false, - androidApplyAudioAttributes: false, - handleAudioSessionActivation: false, - ); + final player = AudioPlayer(); + final String url = + "https://s3.amazonaws.com/scifri-episodes/scifri20181123-episode.mp3"; + PlayerState playerState = PlayerState.stopped; @override void initState() { @@ -33,41 +46,41 @@ class _MyAppState extends State { // Listen to audio interruptions and pause or duck as appropriate. _handleInterruptions(audioSession); // Use another plugin to load audio to play. - await _player.setUrl( - "https://s3.amazonaws.com/scifri-episodes/scifri20181123-episode.mp3"); }); + player.play(UrlSource(url)); } void _handleInterruptions(AudioSession audioSession) { - // just_audio can handle interruptions for us, but we have disabled that in - // order to demonstrate manual configuration. bool playInterrupted = false; audioSession.becomingNoisyEventStream.listen((_) { - print('PAUSE'); - _player.pause(); + print('AudioSession PAUSE'); + player.pause(); }); - _player.playingStream.listen((playing) { + player.onPlayerStateChanged.listen((event) { + print('AudioSession state changed: $event'); playInterrupted = false; - if (playing) { + if (event == PlayerState.playing) { audioSession.setActive(true); } + setState(() { + this.playerState = event; + }); }); + audioSession.interruptionEventStream.listen((event) { - print('interruption begin: ${event.begin}'); - print('interruption type: ${event.type}'); + print('AudioSession interruption begin: ${event.begin}'); + print('AudioSession interruption type: ${event.type}'); if (event.begin) { switch (event.type) { case AudioInterruptionType.duck: if (audioSession.androidAudioAttributes!.usage == - AndroidAudioUsage.game) { - _player.setVolume(_player.volume / 2); - } + AndroidAudioUsage.game) {} playInterrupted = false; break; case AudioInterruptionType.pause: case AudioInterruptionType.unknown: - if (_player.playing) { - _player.pause(); + if (this.playerState == PlayerState.playing) { + player.pause(); playInterrupted = true; } break; @@ -75,11 +88,10 @@ class _MyAppState extends State { } else { switch (event.type) { case AudioInterruptionType.duck: - _player.setVolume(min(1.0, _player.volume * 2)); playInterrupted = false; break; case AudioInterruptionType.pause: - if (playInterrupted) _player.play(); + if (playInterrupted) player.play(UrlSource(url)); playInterrupted = false; break; case AudioInterruptionType.unknown: @@ -89,11 +101,16 @@ class _MyAppState extends State { } }); audioSession.devicesChangedEventStream.listen((event) { - print('Devices added: ${event.devicesAdded}'); - print('Devices removed: ${event.devicesRemoved}'); + print('AudioSession Devices added: ${event.devicesAdded}'); + print('AudioSession Devices removed: ${event.devicesRemoved}'); }); } + Future play() async { + await player.stop(); + await player.play(UrlSource(url)); + } + @override Widget build(BuildContext context) { return MaterialApp( @@ -107,29 +124,21 @@ class _MyAppState extends State { children: [ Expanded( child: Center( - child: StreamBuilder( - stream: _player.playerStateStream, - builder: (context, snapshot) { - final playerState = snapshot.data; - if (playerState?.processingState != - ja.ProcessingState.ready) { - return Container( - margin: EdgeInsets.all(8.0), - width: 64.0, - height: 64.0, - child: CircularProgressIndicator(), - ); - } else if (playerState?.playing == true) { + child: Builder( + builder: (context) { + if (this.playerState == PlayerState.playing) { + print("AudioSession playing"); return IconButton( icon: Icon(Icons.pause), iconSize: 64.0, - onPressed: _player.pause, + onPressed: player.pause, ); } else { + print("AudioSession stopped"); return IconButton( icon: Icon(Icons.play_arrow), iconSize: 64.0, - onPressed: _player.play, + onPressed: play, ); } }, diff --git a/example/lib/ohos_main.dart b/example/lib/ohos_main.dart deleted file mode 100644 index 69ff2379a003f8177fb50159e796e058e315af07..0000000000000000000000000000000000000000 --- a/example/lib/ohos_main.dart +++ /dev/null @@ -1,187 +0,0 @@ -// -// Copyright (c) 2024 Hunan OpenValley Digital Industry Development Co., Ltd. -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// - -import 'dart:math'; - -import 'package:audio_session/audio_session.dart'; -import 'package:flutter/foundation.dart'; -import 'package:flutter/material.dart'; -import 'package:audioplayers/audioplayers.dart'; - -void main() { - runApp(MyApp()); -} - -class MyApp extends StatefulWidget { - @override - _MyAppState createState() => _MyAppState(); -} - -class _MyAppState extends State { - final player = AudioPlayer(); - final String url = - "https://s3.amazonaws.com/scifri-episodes/scifri20181123-episode.mp3"; - PlayerState playerState = PlayerState.stopped; - - @override - void initState() { - super.initState(); - AudioSession.instance.then((audioSession) async { - // This line configures the app's audio session, indicating to the OS the - // type of audio we intend to play. Using the "speech" recipe rather than - // "music" since we are playing a podcast. - await audioSession.configure(AudioSessionConfiguration.speech()); - // Listen to audio interruptions and pause or duck as appropriate. - _handleInterruptions(audioSession); - // Use another plugin to load audio to play. - }); - player.play(UrlSource(url)); - } - - void _handleInterruptions(AudioSession audioSession) { - bool playInterrupted = false; - audioSession.becomingNoisyEventStream.listen((_) { - print('AudioSession PAUSE'); - player.pause(); - }); - player.onPlayerStateChanged.listen((event) { - print('AudioSession state changed: $event'); - playInterrupted = false; - if (event == PlayerState.playing) { - audioSession.setActive(true); - } - setState(() { - this.playerState = event; - }); - }); - - audioSession.interruptionEventStream.listen((event) { - print('AudioSession interruption begin: ${event.begin}'); - print('AudioSession interruption type: ${event.type}'); - if (event.begin) { - switch (event.type) { - case AudioInterruptionType.duck: - if (audioSession.androidAudioAttributes!.usage == - AndroidAudioUsage.game) {} - playInterrupted = false; - break; - case AudioInterruptionType.pause: - case AudioInterruptionType.unknown: - if (this.playerState == PlayerState.playing) { - player.pause(); - playInterrupted = true; - } - break; - } - } else { - switch (event.type) { - case AudioInterruptionType.duck: - playInterrupted = false; - break; - case AudioInterruptionType.pause: - if (playInterrupted) player.play(UrlSource(url)); - playInterrupted = false; - break; - case AudioInterruptionType.unknown: - playInterrupted = false; - break; - } - } - }); - audioSession.devicesChangedEventStream.listen((event) { - print('AudioSession Devices added: ${event.devicesAdded}'); - print('AudioSession Devices removed: ${event.devicesRemoved}'); - }); - } - - Future play() async { - await player.stop(); - await player.play(UrlSource(url)); - } - - @override - Widget build(BuildContext context) { - return MaterialApp( - home: Scaffold( - appBar: AppBar( - title: const Text('audio_session example'), - ), - body: SafeArea( - child: Column( - crossAxisAlignment: CrossAxisAlignment.stretch, - children: [ - Expanded( - child: Center( - child: Builder( - builder: (context) { - if (this.playerState == PlayerState.playing) { - print("AudioSession playing"); - return IconButton( - icon: Icon(Icons.pause), - iconSize: 64.0, - onPressed: player.pause, - ); - } else { - print("AudioSession stopped"); - return IconButton( - icon: Icon(Icons.play_arrow), - iconSize: 64.0, - onPressed: play, - ); - } - }, - ), - ), - ), - Expanded( - child: FutureBuilder( - future: AudioSession.instance, - builder: (context, snapshot) { - final session = snapshot.data; - if (session == null) return SizedBox(); - return StreamBuilder>( - stream: session.devicesStream, - builder: (context, snapshot) { - final devices = snapshot.data ?? {}; - return Column( - crossAxisAlignment: CrossAxisAlignment.center, - children: [ - Text("Input devices", - style: Theme.of(context).textTheme.titleLarge), - for (var device - in devices.where((device) => device.isInput)) - Text( - '${device.name} (${describeEnum(device.type)})'), - SizedBox(height: 16), - Text("Output devices", - style: Theme.of(context).textTheme.titleLarge), - for (var device - in devices.where((device) => device.isOutput)) - Text( - '${device.name} (${describeEnum(device.type)})'), - ], - ); - }, - ); - }, - ), - ), - ], - ), - ), - ), - ); - } -} diff --git a/example/pubspec.yaml b/example/pubspec.yaml index 169d9c0ab93b694c5e078b317862b3ce85da4237..16ec974dd76b45744e3536ad28f9111f8e6938af 100644 --- a/example/pubspec.yaml +++ b/example/pubspec.yaml @@ -11,14 +11,12 @@ environment: dependencies: flutter: sdk: flutter - just_audio: ^0.9.31 audio_session: path: ../ - # please rename ohos_main.dart to main.dart in ohos, and use audioplayers instead just_audio - # audioplayers: - # git: - # url: https://gitee.com/openharmony-sig/flutter_audioplayers.git - # path: packages/audioplayers + audioplayers: + git: + url: https://gitee.com/openharmony-sig/flutter_audioplayers.git + path: packages/audioplayers dev_dependencies: flutter_test: