apache / cordova-plugin-media

Apache Cordova Media Plugin
https://cordova.apache.org/
Apache License 2.0
387 stars 766 forks source link

Play wont work in Android 26 #260

Open jackie-d opened 5 years ago

jackie-d commented 5 years ago

Bug Report

Problem

11-15 16:15:45.634 27694 28035 W AudioManager: Use of stream types is deprecated for operations other than volume control 11-15 16:15:45.634 27694 28035 W AudioManager: See the documentation of requestAudioFocus() for what to use instead with android.media.AudioAttributes to qualify your playback use case

What is expected to happen?

Sounds play on .play()

What does actually happen?

Error thrown, no sound.

Information

Ionic:

   Ionic CLI                     : 5.4.5
   Ionic Framework               : @ionic/angular 4.11.3
   @angular-devkit/build-angular : 0.13.9
   @angular-devkit/schematics    : 7.3.9
   @angular/cli                  : 7.3.9
   @ionic/angular-toolkit        : 1.5.1

Cordova:

   Cordova CLI       : 9.0.0 (cordova-lib@9.0.1)
   Cordova Platforms : android 8.1.0, browser 6.0.0, ios 5.0.1
   Cordova Plugins   : cordova-plugin-ionic-keyboard 2.1.3, cordova-plugin-ionic-webview 4.1.1, (and 16 other plugins)

Utility:

   cordova-res : 0.6.0 (update available: 0.8.1)
   native-run  : 0.2.9 

System:

   Android SDK Tools : 26.1.1 (/Users/jack/Library/Android/sdk)
   ios-deploy        : 1.9.4
   ios-sim           : 8.0.2
   NodeJS            : v8.16.0 (/usr/local/bin/node)
   npm               : 6.13.0
   OS                : macOS Catalina
   Xcode             : Xcode 11.1 Build version 11A1027

Command or Code

let media = new Media('path');
media.play();

Environment, Platform, Device

Version information

Checklist

breautek commented 5 years ago

11-15 16:15:45.634 27694 28035 W AudioManager: Use of stream types is deprecated for operations other than volume control 11-15 16:15:45.634 27694 28035 W AudioManager: See the documentation of requestAudioFocus() for what to use instead with android.media.AudioAttributes to qualify your playback use case

These are warnings, not errors. The W in this log stands for warning.

Error thrown, no sound.

Can you provide the error? The third parameter in Media constructor accepts an error callback.

new Media(src, mediaSuccess, [mediaError], [mediaStatus]);

These are the media error codes definitions.

jackie-d commented 5 years ago

Hi, I already switched out to ionic cordova-plugin-nativeaudio, but I'll give it a try again and let you know as soon as I'll have time.

Thanks

jackie-d commented 5 years ago

Hello tested again an it gives me a couple of errors:

Codes: 0 and 1

I see 1 it's "MediaError.MEDIA_ERR_ABORTED = 1" (maybe because i'm not providing the path in the form of "cvd://..." but just "assets/mymedia.mp3"?

And what about code 0?

Thanks

jackie-d commented 5 years ago

May be interesting to note that .record() method works.

breautek commented 5 years ago

I'm suspecting that it's failing in https://github.com/apache/cordova-plugin-media/blob/acffc0c7b16856e888c932628750fc8b29f1db3d/src/android/AudioPlayer.java#L606

Where it will return the MEDIA_ERR_ABORTED on several caught Exception errors... without actually printing out the errors (yuck...)

For debugging purposes, you should be able to find this file in the following path:

<cordova-project>/platforms/android/app/src/main/java/org/apache/cordova/media/AudioPlayer.java

Then I'd recommend adding the following line to each of the catch blocks.

Log.e(LOG_TAG, "exception", e);

It won't fix the MEDIA_ERR_ABORTED but will hopefully give some more insight in why a MEDIA_ERR_ABORTED is being passed down inside the android logcat.

jackie-d commented 5 years ago

Hey, i've done what you asked but no additional log comes out.

11-21 09:52:52.743 20809 21349 W AudioManager: Use of stream types is deprecated for operations other than volume control
11-21 09:52:52.743 20809 21349 W AudioManager: See the documentation of requestAudioFocus() for what to use instead with android.media.AudioAttributes to qualify your playback use case
11-21 09:52:52.745  2013  3400 I MediaFocusControl: requestAudioFocus() from uid/pid 10308/20809 clientId=android.media.AudioManager@a319373org.apache.cordova.media.AudioHandler$1@ffb0230 callingPack=com.aaa.myapp req=1 flags=0x0 sdk=28
11-21 09:52:52.750 20809 20809 D SystemWebChromeClient: http://localhost:8100/consolelogs.js: Line 50 : error sound:  {"code":1}
11-21 09:52:52.750 20809 20809 I chromium: [INFO:CONSOLE(50)] "error sound:  {"code":1}", source: http://localhost:8100/consolelogs.js (50)

11-21 09:52:52.754 20809 21349 V MediaPlayer: cleanDrmObj: mDrmObj=null mDrmSessionId=null
11-21 09:52:52.757 20809 20809 D SystemWebChromeClient: http://localhost:8100/consolelogs.js: Line 50 : error sound:  {"code":0}
11-21 09:52:52.758 20809 20809 I chromium: [INFO:CONSOLE(50)] "error sound:  {"code":0}", source: http://localhost:8100/consolelogs.js (50)
11-21 09:52:52.789  2013  3400 D PerfShielderService: com.aaa.myapp|com.aaa.myapp/com.aaa.myapp.MainActivity|165|74463027058713|150|5|2

Btw, this is the code i'm relying on to execute, may be some async problem with completion callback to release resource?

  private doPlayIOs( clipname: string ) {
    const samplePath = this.getSamplePath(clipname);
    this.media = new Media(samplePath,
          () => {
            this.unloadIOs();
          },
          (error) => {
            console.log('error sound: ', JSON.stringify(error));
            this.unloadIOs();
          }
      );

    this.media.play();
  }

  private unloadIOs() {
    if ( this.media ) {
      try {
        this.media.stop();
        this.media.release();
      } catch ( error ) {
        console.log('error releasing media: ', error);
      }
    }
  }
breautek commented 5 years ago

Btw, this is the code i'm relying on to execute, may be some async problem with completion callback to release resource?

I don't see any potential for async race conditions here... but to confirm race condition suspicions I try to wrap my suspect inside a setTimeout to delay by 1 second or two. Usually if the problem goes away, then there could be a race condition.

Hey, i've done what you asked but no additional log comes out.

My assumptions might have been wrong. If possible are you able to provide a sample reproduction app that contains only what is required to reproduce this issue (pure cordova, without ionic, without your app code, etc). I'd be able to experiment with it further.

markwaldin commented 3 years ago

I've spent more than a day chasing this. Nothing plays and I get no error condition returned at all. I am using a simple test of just loading the file on device ready and running play. I may be confused as to how to specify access to the sound file. The file is in 'www' and I have tried accessing it using './soundfile', 'file:///android_asset/www/soundfile'.