Browse Source

控制音量的代码

Caijinglong 6 years ago
parent
commit
5b0df0628a

+ 8 - 0
README.md

@@ -2,6 +2,14 @@
 
 ijkplayer,通过纹理的方式接入 bilibili/ijkplayer
 
+## Progress
+
+目前正处于初始开发阶段
+
+最初准备参考官方video_player的api方式进行开发,但是觉得调用的方式比较奇怪
+
+需要自定义LifeCycle进行管理,而且自定义控制器不太方便,遂决定重写api的代码结构
+
 ## TodoList
 
 - [X] 控制器逻辑

+ 14 - 1
android/src/main/java/top/kikt/ijkplayer/Ijk.kt

@@ -85,6 +85,11 @@ class Ijk(private val registry: PluginRegistry.Registrar) : MethodChannel.Method
                 }
                 result?.success(true)
             }
+            "setVolume" -> {
+                val volume = call.argument<Int>("volume")
+                setVolume(volume)
+                result?.success(true)
+            }
             else -> {
                 result?.notImplemented()
             }
@@ -176,9 +181,17 @@ class Ijk(private val registry: PluginRegistry.Registrar) : MethodChannel.Method
         mediaPlayer.stop()
     }
 
-    fun seekTo(msec: Long) {
+    private fun seekTo(msec: Long) {
         mediaPlayer.seekTo(msec)
     }
 
+    private fun setVolume(volume: Int?) {
+        if (volume == null) {
+            return
+        }
+        val v = volume.toFloat() / 100
+        ijkPlayer.setVolume(v, v)
+    }
+
 }
 

+ 18 - 0
example/lib/main.dart

@@ -66,6 +66,7 @@ class HomePageState extends State<HomePage> {
             ),
             _buildPlayAssetButton(),
             _buildControllerButtons(),
+            _buildVolumeBar(),
           ],
         ),
       ),
@@ -192,4 +193,21 @@ class HomePageState extends State<HomePage> {
       ],
     );
   }
+
+  int volume = 100;
+
+  _buildVolumeBar() {
+    return Slider(
+      value: volume / 100,
+      label: (volume * 100).toInt().toString(),
+      onChanged: (double value) {
+        var targetVolume = (value * 100).toInt();
+        print("target volume = $targetVolume");
+        controller.setVolume(targetVolume);
+        setState(() {
+          this.volume = targetVolume;
+        });
+      },
+    );
+  }
 }

+ 2 - 2
ios/Classes/FlutterIJK.h → ios/Classes/CoolFlutterIJK.h

@@ -4,9 +4,9 @@
 
 #import <Foundation/Foundation.h>
 #import <Flutter/Flutter.h>
-#import "KKIjkNotifyChannel.h"
+#import "CoolIjkNotifyChannel.h"
 
-@interface FlutterIJK : NSObject
+@interface CoolFlutterIJK : NSObject
 
 @property(nonatomic, strong) NSObject <FlutterPluginRegistrar> *registrar;
 

+ 18 - 10
ios/Classes/FlutterIJK.m → ios/Classes/CoolFlutterIJK.m

@@ -2,25 +2,25 @@
 // Created by Caijinglong on 2019-03-08.
 //
 
-#import "FlutterIJK.h"
-#import "KKVideoInfo.h"
-#import "KKIjkNotifyChannel.h"
+#import "CoolFlutterIJK.h"
+#import "CoolVideoInfo.h"
+#import "CoolIjkNotifyChannel.h"
 #import <IJKMediaFramework/IJKMediaFramework.h>
 #import <IJKMediaFramework/IJKMediaPlayer.h>
 #import <AVFoundation/AVFoundation.h>
 #import <libkern/OSAtomic.h>
 
-@interface FlutterIJK () <FlutterTexture, KKIjkNotifyDelegate>
+@interface CoolFlutterIJK () <FlutterTexture, KKIjkNotifyDelegate>
 @end
 
-@implementation FlutterIJK {
+@implementation CoolFlutterIJK {
     int64_t textureId;
     CADisplayLink *displayLink;
     NSObject <FlutterTextureRegistry> *textures;
     IJKFFMoviePlayerController *controller;
     CVPixelBufferRef latestPixelBuffer;
     FlutterMethodChannel *channel;
-    KKIjkNotifyChannel *notifyChannel;
+    CoolIjkNotifyChannel *notifyChannel;
 }
 
 - (instancetype)initWithRegistrar:(NSObject <FlutterPluginRegistrar> *)registrar {
@@ -96,13 +96,21 @@
         [self seekTo:target];
         result(@(YES));
     } else if ([@"getInfo" isEqualToString:call.method]) {
-        KKVideoInfo *info = [self getInfo];
+        CoolVideoInfo *info = [self getInfo];
         result([info toMap]);
+    } else if ([@"setVolume" isEqualToString:call.method]) {
+        NSDictionary *params = [self params:call];
+        float v = [params[@"volume"] floatValue] / 100;
+        controller.playbackVolume = v;
+        result(@(YES));
     } else {
         result(FlutterMethodNotImplemented);
     }
 }
 
+- (NSDictionary *)params:(FlutterMethodCall *)call {
+
+}
 
 + (instancetype)ijkWithRegistrar:(NSObject <FlutterPluginRegistrar> *)registrar {
     return [[self alloc] initWithRegistrar:registrar];
@@ -154,7 +162,7 @@
     [displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
     displayLink.paused = YES;
 
-    notifyChannel = [KKIjkNotifyChannel channelWithController:controller textureId:textureId registrar:self.registrar];
+    notifyChannel = [CoolIjkNotifyChannel channelWithController:controller textureId:textureId registrar:self.registrar];
     notifyChannel.infoDelegate = self;
 }
 
@@ -202,8 +210,8 @@
     return NULL;
 }
 
-- (KKVideoInfo *)getInfo {
-    KKVideoInfo *info = [KKVideoInfo new];
+- (CoolVideoInfo *)getInfo {
+    CoolVideoInfo *info = [CoolVideoInfo new];
 
     CGSize size = [controller naturalSize];
     NSTimeInterval duration = [controller duration];

+ 3 - 3
ios/Classes/FlutterIjkManager.h → ios/Classes/CoolFlutterIjkManager.h

@@ -5,9 +5,9 @@
 #import <Foundation/Foundation.h>
 #import <Flutter/Flutter.h>
 
-@class FlutterIJK;
+@class CoolFlutterIJK;
 
-@interface FlutterIjkManager : NSObject
+@interface CoolFlutterIjkManager : NSObject
 
 @property(nonatomic, strong) NSObject <FlutterPluginRegistrar> *registrar;
 
@@ -17,7 +17,7 @@
 
 - (int64_t)create;
 
-- (FlutterIJK *)findIJKWithId:(int64_t)id1;
+- (CoolFlutterIJK *)findIJKWithId:(int64_t)id1;
 
 - (void)disposeWithId:(int64_t)id;
 @end

+ 7 - 7
ios/Classes/FlutterIjkManager.m → ios/Classes/CoolFlutterIjkManager.m

@@ -2,12 +2,12 @@
 // Created by Caijinglong on 2019-03-08.
 //
 
-#import "FlutterIjkManager.h"
-#import "FlutterIJK.h"
+#import "CoolFlutterIjkManager.h"
+#import "CoolFlutterIJK.h"
 #import <IJKMediaFramework/IJKMediaFramework.h>
 
-@implementation FlutterIjkManager {
-    NSMutableDictionary<NSNumber *, FlutterIJK * > *dict;
+@implementation CoolFlutterIjkManager {
+    NSMutableDictionary<NSNumber *, CoolFlutterIJK * > *dict;
 }
 
 
@@ -26,18 +26,18 @@
 }
 
 - (int64_t)create {
-    FlutterIJK *ijk = [FlutterIJK ijkWithRegistrar:self.registrar];
+    CoolFlutterIJK *ijk = [CoolFlutterIJK ijkWithRegistrar:self.registrar];
     NSNumber *number = @([ijk id]);
     dict[number] = ijk;
     return [ijk id];
 }
 
-- (FlutterIJK *)findIJKWithId:(int64_t)id {
+- (CoolFlutterIJK *)findIJKWithId:(int64_t)id {
     return dict[@(id)];
 }
 
 - (void)disposeWithId:(int64_t)id {
-    FlutterIJK *ijk = dict[@(id)];
+    CoolFlutterIJK *ijk = dict[@(id)];
     if (ijk) {
         [ijk dispose];
         [dict removeObjectForKey:@(id)];

+ 3 - 3
ios/Classes/KKIjkNotifyChannel.h → ios/Classes/CoolIjkNotifyChannel.h

@@ -6,13 +6,13 @@
 #import <Flutter/Flutter.h>
 #import <IJKMediaFramework/IJKMediaFramework.h>
 
-@class KKVideoInfo;
+@class CoolVideoInfo;
 
 @protocol KKIjkNotifyDelegate
-- (KKVideoInfo *)getInfo;
+- (CoolVideoInfo *)getInfo;
 @end
 
-@interface KKIjkNotifyChannel : NSObject
+@interface CoolIjkNotifyChannel : NSObject
 
 @property(nonatomic, strong) IJKFFMoviePlayerController *controller;
 

+ 3 - 3
ios/Classes/KKIjkNotifyChannel.m → ios/Classes/CoolIjkNotifyChannel.m

@@ -2,11 +2,11 @@
 // Created by Caijinglong on 2019-03-15.
 //
 
-#import "KKIjkNotifyChannel.h"
-#import "KKVideoInfo.h"
+#import "CoolIjkNotifyChannel.h"
+#import "CoolVideoInfo.h"
 
 
-@implementation KKIjkNotifyChannel {
+@implementation CoolIjkNotifyChannel {
     FlutterMethodChannel *channel;
 }
 

+ 1 - 1
ios/Classes/KKVideoInfo.h → ios/Classes/CoolVideoInfo.h

@@ -5,7 +5,7 @@
 #import <Foundation/Foundation.h>
 
 
-@interface KKVideoInfo : NSObject
+@interface CoolVideoInfo : NSObject
 
 @property(nonatomic, assign) NSTimeInterval duration;
 

+ 2 - 2
ios/Classes/KKVideoInfo.m → ios/Classes/CoolVideoInfo.m

@@ -2,10 +2,10 @@
 // Created by Caijinglong on 2019-03-15.
 //
 
-#import "KKVideoInfo.h"
+#import "CoolVideoInfo.h"
 
 
-@implementation KKVideoInfo {
+@implementation CoolVideoInfo {
 
 }
 

+ 4 - 4
ios/Classes/IjkplayerPlugin.m

@@ -1,6 +1,6 @@
 #import "IjkplayerPlugin.h"
-#import "FlutterIjkManager.h"
-#import "FlutterIJK.h"
+#import "CoolFlutterIjkManager.h"
+#import "CoolFlutterIJK.h"
 
 @interface FlutterMethodCall (Ijk)
 - (int64_t)getId;
@@ -13,7 +13,7 @@
 static IjkplayerPlugin *__sharedInstance;
 
 @implementation IjkplayerPlugin {
-    FlutterIjkManager *manager;
+    CoolFlutterIjkManager *manager;
 
 }
 
@@ -26,7 +26,7 @@ static IjkplayerPlugin *__sharedInstance;
     self = [super init];
     if (self) {
         self.registrar = registrar;
-        manager = [FlutterIjkManager managerWithRegistrar:registrar];
+        manager = [CoolFlutterIjkManager managerWithRegistrar:registrar];
     }
 
     return self;

+ 10 - 0
lib/src/controller.dart

@@ -153,6 +153,10 @@ class IjkMediaController {
     }
   }
 
+  Future<void> setVolume(int volume) async {
+    await _plugin.setVolume(volume);
+  }
+
   Future<void> stop() async {
 //    await _plugin?.stop();
 //    refreshVideoInfo();
@@ -234,4 +238,10 @@ class _IjkPlugin {
       "target": target,
     });
   }
+
+  Future<void> setVolume(int volume) async {
+    await channel.invokeMethod("setVolume", <String, dynamic>{
+      "volume": volume,
+    });
+  }
 }