2011-08-11 68 views
2

我陷入了一個奇怪的問題。我有一個MediaPlayer應用程序可以播放剛纔錄製的音頻文件。起初mediaplayer初始化好了,文件可以播放。Android:媒體文件從文件系統中消失

當我旋轉屏幕時,活動被破壞,然後重新初始化爲新的方向。因此,我也重新初始化了媒體播放器。

這部作品了幾次,但在某些時候mediaPlayer.setDataSource()拋出一個NullPointerException,因爲該文件突然消失了。可悲的是,我在日誌中沒有看到任何其他錯誤。

下面是一些代碼片段:

玩家創造:

/** 
* Creates and initializes the player with the proper file. 
*/ 
private void createPlayer() { 
    synchronized (playerMutex) { 
     player = new MediaPlayer(); 
     player.setLooping(false); 
     player.setOnPreparedListener(this); 
     player.setOnErrorListener(this); 
     player.setOnCompletionListener(this); 
    } 
    readGreeting(); 
} 

播放器初始化:

isPrepared = false; 
    try { 
     final File file = new File(audioFilename); 

     in = new FileInputStream(file); 
     synchronized (playerMutex) { 
      player.setDataSource(in.getFD()); 
     } 

     // using a runnable instead of prepareAsync to not accidentally call pause on media player while preparing 
     Runnable preparer = new Runnable() { 
      @Override 
      public void run() { 
       try { 
        synchronized (playerMutex) { 
         if (player != null) { 
          player.prepare(); 
         } 
        } 
       } catch (Exception ex) { 
        Log.e(TAG, "Error preparing player for file " + file.getAbsolutePath(), ex); 
       } 
      } 
     }; 
     new Thread(preparer).start(); 
    } catch (Exception ex) { 
     btnPlayback.setEnabled(false); 
     Log.e(TAG, "Error preparing player", ex); 
    } finally { 
     if (in != null) { 
      try { 
       in.close(); 
      } catch (IOException e) { 
       Log.e(TAG, "initPlayer: ", e); 
      } 
     } 
    } 

節能實例狀態...

@Override 
protected void onPause() { 
    synchronized (playerMutex) { 

     if (isPlaying()) { 
      getIntent().putExtra(EXTRA_KEY_SEEK, player.getCurrentPosition()); 
      pause(); 
     } 
    } 
    setAudioModeBackToNormal(); 
    super.onPause(); 
} 

private void pause() { 
    synchronized (playerMutex) { 
     if (isPlaying()) { 
      player.pause(); 
     } 
    } 
    btnPlayback.setVisibility(View.VISIBLE); 
    btnPause.setVisibility(View.GONE); 
} 

@Override 
protected void onSaveInstanceState(Bundle outState) { 
    final Bundle extras = getIntent().getExtras(); 

    outState.putBundle("extras", extras); 
    super.onSaveInstanceState(outState); 
} 

@Override 
protected void onRestoreInstanceState(Bundle savedInstanceState) { 
    super.onRestoreInstanceState(savedInstanceState); 
    getIntent().putExtras(savedInstanceState.getBundle("extras")); 
} 

cleanu p:

private void stopPlayerAndFreeResources() { 
    synchronized (playerMutex) { 
     isPrepared = false; 
     if (player != null) { 
      player.stop(); 
      player.release(); 
      player = null; 
     } 
    } 
    if (in != null) { 
     try { 
      in.close(); 
      in = null; 
     } catch (IOException e) { 
      Log.e(TAG, "Unexpected error", e); 
     } 
    } 
} 

也許我從錯誤的角度看待問題,它與玩家無關。有沒有人有消失文件的問題?

+1

是否與您的線程有關,最後一個線程File()還沒有放開對該文件的讀取訪問? – Blundell

+0

這可能是某種東西這樣,但我找不到在哪裏如果你看看播放器初始化代碼片斷,你看,這個文件輸入流調用player.setDataSource(in.getFD())後立即關閉。 –

+0

UPS,wron按鈕......我沒有我 –

回答

1

我有播放列表文件消失一次。原因是某些媒體播放器可以選擇管理我的播放列表。在這種情況下,管理意味着刪除我已經有的播放列表:(