ukasz123 / soundpool

Soundpool plugin for Flutter
88 stars 63 forks source link

How do I set what audio channel to use? #63

Closed johnareid54 closed 3 years ago

johnareid54 commented 3 years ago

Great package, but I noticed on Android phone the sound is coming through the phone's ring tone and not the media channel. The result is you cannot use headphones on the phone.

Is there a way to define which channel the phone should use?

ukasz123 commented 3 years ago

@johnareid54 I don't understand. Have you created your Soundpool instance with StreamType.music? I assume you want to control which audio output your app should use. Is that what you need?

How can I test the problem you've described?

johnareid54 commented 3 years ago

I agree it is a strange problem. It looks similar to the issue Sounds don't play on iOS physical device #28 It was apparently using ringer volume,

My code is very simple but works very well! I just have to find a way to make it uses the media audio on both Android and IOS.

`import 'dart:async';
import 'package:soundpool/soundpool.dart';
import 'package:flutter/services.dart';

class SoundAudio {
  Soundpool _soundpool;
  bool loop = false;
  int _correct;
  int _wrong;
  int _word;
  Timer _timer;

  Future<void> load() async {
    _soundpool = Soundpool(streamType: StreamType.notification);
    var asset = await rootBundle.load("assets/audio/right.mp3");
    _correct = await _soundpool.load(asset);
    asset = await rootBundle.load("assets/audio/wrong.mp3");
    _wrong = await _soundpool.load(asset);
  }

  Future<void> correct() async {
    _soundpool.stop(_word);
    _timer?.cancel();
    loop = false;
    await _soundpool.play(_correct);
  }

  Future<void> wrong() async {
    await _soundpool.play(_wrong);
  }

  Future<bool> loadWord(int file) async {
    _timer?.cancel();
    loop = false;
    String subfolder = "0";
    if (file > 99) subfolder = file.toString().substring(0, 1);
    var asset = await rootBundle.load("assets/words/$subfolder/$file.mp3");
    _word = await _soundpool.load(asset);
    return true;
  }

  Future<void> word() async {
    await _soundpool.play(_word);
  }

  Future<void> correctword() async {
    await _soundpool.play(_correct);
    await _soundpool.play(_word);
  }

  Future<void> loadPlay(int file) async {
    await loadWord(file);
    repeat(3);
  }

  void repeat(int seconds) {
    loop = false;
    Future.delayed(Duration(seconds: 1), () {
      loop = true;
      word();
      _timer = Timer.periodic(Duration(seconds: seconds), (timer) {
        word();
      });
    });
  }

  stop() {
    _soundpool.stop(_word);
    _timer?.cancel();
  }

  Future<void> test() async {
    var asset = await rootBundle.load("assets/audio/190.mp3");
    await _soundpool.loadAndPlay(asset);
  }

  SoundAudio() {
    load();
  }

  void dispose() {
    _soundpool.dispose();
    _timer?.cancel();
  }
}

`

johnareid54 commented 3 years ago

I found the solution to this issue - the channel is controlled by this line-- _soundpool = Soundpool(streamType: StreamType.notification); Instead of StreamType.notification) change it to StreamType.music.