2013-08-14 28 views
5

Tôi đang sử dụng registerMediaRouteProvider và nó cung cấp cho bạn thanh âm lượng để cập nhật âm lượng của TV. Tôi thực hiện MediaRouteAdapter và khi tôi chà thanh âm lượng, âm lượng thay đổi, nhưng thanh ui của thanh âm lượng luôn đặt lại về 0. Làm cách nào để cập nhật ui của thanh âm lượng khi âm lượng thay đổi?Cách cập nhật thanh âm lượng trong MinimalMediaRouteProvider

@Override 
public void onCreate(Bundle savedInstanceState) { 

    mCastContext = new CastContext(getApplicationContext()); 
    MediaRouteHelper.registerMinimalMediaRouteProvider(mCastContext, this); 
    mMediaRouter = MediaRouter.getInstance(getApplicationContext()); 
    mMediaRouteSelector = MediaRouteHelper.buildMediaRouteSelector(MediaRouteHelper.CATEGORY_CAST); 
    mMetaData = new ContentMetadata(); 

    mMediaRouterCallback = new MyMediaRouterCallback(); 
    mMediaRouteButton.setRouteSelector(mMediaRouteSelector); 
} 

private class MyMediaRouterCallback extends MediaRouter.Callback { 
    @Override 
    public void onRouteSelected(MediaRouter router, RouteInfo route) { 
     MediaRouteHelper.requestCastDeviceForRoute(route); 
    } 

    @Override 
    public void onRouteUnselected(MediaRouter router, RouteInfo route) { 
     try { 
      if (mSession != null) { 
       Log.e(TAG, "Ending session and stopping application"); 
       mSession.setStopApplicationWhenEnding(true); 
       mSession.endSession(); 
      } else { 
       Log.e(TAG, "onRouteUnselected: mSession is null"); 
      } 
     } catch (IllegalStateException e) { 
      Log.e(TAG, "onRouteUnselected:"); 
      e.printStackTrace(); 
     } catch (IOException e) { 
      Log.e(TAG, "onRouteUnselected:"); 
      e.printStackTrace(); 
     } 
     mSelectedDevice = null; 
    } 
} 

@Override 
public void onDeviceAvailable(CastDevice device, String arg1, MediaRouteStateChangeListener listener) { 
    mSelectedDevice = device; 
    openSession(); 
} 

@Override 
public void onSetVolume(double volume) { 
    try { 
     if (mMessageStream != null) { 
      mMessageStream.setVolume(volume); 
     } 
    } catch (IllegalStateException e) { 
     Log.e(TAG, "Problem sending Set Volume", e); 
    } catch (IOException e) { 
     Log.e(TAG, "Problem sending Set Volume", e); 
    } 
} 

@Override 
public void onUpdateVolume(double volumeChange) { 
    try { 
     if (mCurrentRoute != null) { 
      mCurrentRoute.requestUpdateVolume((int) (volumeChange * 20)); 
     } 
    } catch (IllegalStateException e) { 
     Log.e(TAG, "Problem sending Update Volume", e); 
    } 
} 

EDIT - thêm nơi tôi khởi mMessageStream

private void openSession() { 
     mSession = new ApplicationSession(mCastContext, mSelectedDevice); 
     int flags = 0; 
     flags |= ApplicationSession.FLAG_DISABLE_NOTIFICATION; 
     flags |= ApplicationSession.FLAG_DISABLE_LOCK_SCREEN_REMOTE_CONTROL; 
     mSession.setApplicationOptions(flags); 

     Log.d(TAG, "Beginning session with context: " + mCastContext); 
     Log.d(TAG, "The session to begin: " + mSession); 
     mSession.setListener(new com.google.cast.ApplicationSession.Listener() { 

      @Override 
      public void onSessionStarted(ApplicationMetadata appMetadata) { 
       Log.d(TAG, "Getting channel after session start"); 
       ApplicationChannel channel = mSession.getChannel(); 
       if (channel == null) { 
        Log.e(TAG, "channel = null"); 
        return; 
       } 
       Log.d(TAG, "Creating and attaching Message Stream"); 
       mMessageStream = new MediaProtocolMessageStream(); 
       channel.attachMessageStream(mMessageStream); 

       if (mMessageStream.getPlayerState() == null) { 
        if (vastVideoView.getPlayingPlaylistItem() != null) { 
         loadMedia(); 
        } 
       } else { 
        Log.e(TAG, "Found player already running; updating status"); 
       } 
      } 

      @Override 
      public void onSessionStartFailed(SessionError error) { 
       Log.e(TAG, "onStartFailed " + error + " code " + error.getCode()); 
       nowifi.setVisibility(View.GONE); 
      } 

      @Override 
      public void onSessionEnded(SessionError error) { 
       Log.i(TAG, "onEnded " + error); 
       controller.removeChromeCastListener(); 
       controller.setChromeCast(false); 
       nowifi.setVisibility(View.GONE); 
      } 
     }); 

     try { 
      Log.e(TAG, "Starting session with app name " + getString(R.string.app_id)); 
      mSession.startSession(getString(R.string.app_id)); 

      vastVideoView.stopPlayback(); 

      controller = vastVideoView.getMediaController(); 
      controller.setChromeCast(true); 
      controller.setPausePlayListener(pausePlayListener); 
      seekBar = controller.getSeekBar(); 
      seekBar.setProgress(0); 

      mPlayButtonShowsPlay = true; 
     } catch (IOException e) { 
      Log.e(TAG, "Failed to open session", e); 
      controller.removeChromeCastListener(); 
      controller.setChromeCast(false); 
      nowifi.setVisibility(View.GONE); 
     } 
    } 

This is the volume bar that is not updating

Trả lời

1

Ở cấp Receiver, có hai cách để cài đặt âm lượng:

  1. cast.Receiver .Platform.setVolume ([0.0,1.0]) Điều này sẽ đặt tiếng nói lume và hiển thị thanh màu xanh. Đây là những gì thường được gọi bằng thông báo onVolume.

  2. phương tiệnElement.volume = [0.0, 1.0]; Điều này sẽ đặt âm lượng mà không hiển thị thanh màu xanh dương. Bạn có thể sử dụng chức năng này để làm mờ dần/mờ dần và cân bằng.

Bây giờ trên mã Java của bạn: tôi không thấy làm thế nào bạn đang nhận được messageStream của bạn đến những gì bạn đã đăng, nhưng vì nó có thể sẽ là điều đúng đắn. Đây là lines from the reference docs:

công thức MediaProtocolCommand setVolume (khối lượng kép)

Thiết lập âm lượng âm thanh. Tham số khối lượng Âm lượng, trong khoảng 0.0 (0%) đến 1.0 (100%). Trả về Đối tượng lệnh cho yêu cầu này Ném IOException Nếu xảy ra lỗi I/O khi gửi thư. IllegalStateException Nếu luồng này không được đính kèm với kênh được kết nối.

+0

Tôi đang tạo phương thức 'MediaProtocolMessageStream' đối tượng bên trong 'ApplicationSession.Listener onSessionStarted'. Tôi sẽ thêm mã ở trên.Tôi đã có 'MediaProcolCommand'' setVolume' bên trong 'onSetVolume'. Những gì tôi đang tìm kiếm là làm thế nào để cập nhật thanh tìm khối lượng trong hộp thoại bật lên, nơi nó cho bạn thấy thanh âm lượng và nút ngắt kết nối – heero

4

Tôi đã tìm kiếm này trong nhiều ngày và tôi đột nhiên tìm thấy giải pháp của bản thân mình: P

Đầu tiên, bạn sẽ cần một MediaRouteStateChangeListener để xử lý này.

MediaRouteStateChangeListener mRouteStateListener; 

Thứ hai, chỉ định người nghe theo số onDeviceAvailable.

@Override 
public void onDeviceAvailable(CastDevice device, String arg1, MediaRouteStateChangeListener listener) { 
    mSelectedDevice = device; 
    mRouteStateListener = listener; 
    openSession(); 
} 

ngoái, gọi MediaRouteStateListener.onVolumeChanged() trong onSetVolume hoặc onUpdateVolume.

@Override 
public void onSetVolume(double volume) { 
    try { 
     if (mMessageStream != null) { 
      mMessageStream.setVolume(volume); 
      mRouteStateListener.onVolumeChanged(volume); 
     } 
    } catch (IllegalStateException e) { 
     Log.e(TAG, "Problem sending Set Volume", e); 
    } catch (IOException e) { 
     Log.e(TAG, "Problem sending Set Volume", e); 
    } 
} 

Điều này sẽ giúp thanh âm lượng của bạn hoạt động bình thường. :)

+0

Giải pháp của bạn đã hoạt động trên dự án mẫu android-cast. Nhưng khi tôi thay đổi android.support.v7.app.MediaRouteActionProvider "thành mục hành động bằng MediaRouteButton để sử dụng với ActionBarSherlock. Nó không hoạt động. Bạn đã sử dụng actionProviderClass hoặc MediaRouteButton –

0

Điều chỉnh âm lượng được cập nhật của Google vào mùa hè này để chúng được chia sẻ giữa các thiết bị và ứng dụng được kết nối. Xin xem giải pháp của tôi ở đây:

https://stackoverflow.com/a/26554007/672373

Các vấn đề liên quan